const {
	editUrl,
	listUrl,
	teamUrl,
} = require('@utils/util-apis')['projectUrl']
const {
	$U_UserInfo,
	$U_GetItem,
} = require('@utils/storage')
const {
	loadProjectData,
} = require('../api/api-data-2x')
const {
	reqGet,
	reqPost,
} = require('@utils/request')
const {
	setFormEdit,
	setBtnEdit,
	setEditStatus,
	randomId,
	clearTmpProjectData,
} = require('../api/api-utils')

module.exports = {

	/**
	 * 新建项目
	 * @return {[type]} [description]
	 */
	saveMethod() {
		let {
			store,
			dataGrid,
			projectForm,
			dailogComp,
		} = this;
		let {
			formDialog,
		} = dailogComp
		$.messager.progress();
		projectForm.form('submit', {
			url: editUrl,
			onSubmit(param) {
				let isValid = projectForm.form('validate');
				if (!isValid) {
					$.messager.progress('close');
					return false
				}
				let userInfo = $U_UserInfo()
				param.user_id = userInfo.id
				param.create_user = userInfo.realname
				param.flag = store._editFlag
				param.project_id = store._projectId
				return true;
			},
			success() {
				$.messager.progress('close');
				dataGrid.datagrid('reload');
				formDialog.dialog('close');
				projectForm.form('clear')
			}
		})
	},

	onRowContextMenu(e, index, row) {
		e.preventDefault()
		let {
			store,
			projectMenu,
			dataGrid,
		} = this
		if (!row)
			return false
		store.currentRow = row
		projectMenu.menu('show', {
			left: e.pageX,
			top: e.pageY,
		})
		dataGrid.datagrid('selectRow', index)
	},

	/**
	 * 接口测试页面
	 * @return {[type]} [description]
	 */
	testAPIMethod() {
		let {
			mainComp,
			projectComp,
		} = $['components-jq']
		let tabsJQ = mainComp.tabsJQ
		let row = projectComp.store.currentRow
		if (!row && !row.id)
			return false
		let title = '接口测试'
		if (tabsJQ.tabs('exists', title)) {
			tabsJQ.tabs('select', title)
			require('@modules/api-test/api-test-init')(row.id, true)
			return
		}
		import ('@views/apis/api-test.html').then(content => {
			tabsJQ.tabs('add', {
				id: title,
				title,
				content,
			})
			require('@modules/api-test/api-test-init')(row.id)
		})
	},

	/**
	 * 接口管理页面
	 * @param  {[type]} index [description]
	 * @param  {[type]} row   [description]
	 * @return {[type]}       [description]
	 */
	apiManageMethod(index, row) {
		let {
			mainComp,
			projectComp,
			apiComp,
		} = $['components-jq']
		let tabsJQ = mainComp.tabsJQ
		row = row || projectComp.store.currentRow
		if (!row && !row.id)
			return false
		let dataGrid = projectComp.dataGrid
		let title = '接口管理'
		dataGrid.datagrid('loading')
		if (tabsJQ.tabs('exists', title)) {
			$.messager.progress();
			tabsJQ.tabs('select', title);
			// 当前如果有编辑项目需要先保存
			if (apiComp.store && apiComp.store.isLock === true && apiComp.store.projectId != row.id) {
				$.messager.progress('close')
				dataGrid.datagrid('loaded')
				$.messager.alert({
					title: '提示',
					msg: '当前有正在编辑项目，请先保存或取消！'
				})
				return false
			}
			require('@modules/api/init-api')(row.id, true)
			setTimeout(() => {
				$.messager.progress('close')
				dataGrid.datagrid('loaded')
			}, 500);
			return
		}
		// 首次打开接口管理页面
		import ('@views/apis/api.html').then(content => {
			$.messager.progress();
			tabsJQ.tabs('add', {
				id: title,
				title,
				content,
			})
			require('@modules/api/init-api')(row.id)
			setTimeout(() => {
				$.messager.progress('close')
				dataGrid.datagrid('loaded')
				let tepd = $U_GetItem('tmp_edit_project_data')
				let th = $U_GetItem('tmp_history')
				let tcca = $U_GetItem('tmp_current_check_api')
				if (apiComp.store && apiComp.store.isLock === true && tepd && th && tcca) {
					$.messager.confirm({
						title: '提示',
						msg: '上次编辑没有保存，是否继续编辑？',
						fn(r) {
							console.log('r', r)
							if (r) {
								loadProjectData(apiComp, row.id, {
									tmpEditProjectData: tepd,
									tmpHistory: th,
									tmpCurrentCheckApi: tcca,
								})
							} else {
								reqGet('/ult/project/unlock', {
									project_id: apiComp.store.projectId,
								}).then(result => {
									apiComp.store.isLock = false
									clearTmpProjectData()
									setEditStatus(apiComp)
								})
							}
						},
					})
				}
			}, 500);
		})
	},
}